//
//  RCLoginController.m
//  RocketProject
//
//  Created by chen jiedan on 13-8-28.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCLoginController.h"
#import "GloableEngineService.h"


@interface RCLoginController ()

@end

@implementation RCLoginController

@synthesize controllers = _controllers;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (id) initWithViewControllers:(NSArray *)controllers
{
    self = [super init];
    if (self) {
        _controllers = [controllers retain];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
//    self.navigationController.navigationBarHidden = YES;
    
    [self hideContentController:[_controllers objectAtIndex:1]];
    [self displayContentController:[_controllers objectAtIndex:0]];

}


- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    self.navigationController.navigationBarHidden = YES;
    self.titleLabelText = @"我的";
    self.navigationItem.titleView = [self createTitleView];

   
   
    


}

- (void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    self.navigationController.navigationBarHidden = NO;
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




- (CGRect) frameForContentController
{
    return CGRectMake(0, 0, 320, UIScreenHeight-44-20);
}

- (void) displayContentController: (UIViewController*) content;
{
    [self addChildViewController:content];                 // 1
    content.view.frame = [self frameForContentController]; // 2
    [self.view addSubview:content.view];
    [content didMoveToParentViewController:self];          // 3
}


- (void) hideContentController: (UIViewController*) content
{
    [content willMoveToParentViewController:nil];  // 1
    [content.view removeFromSuperview];            // 2
    [content removeFromParentViewController];      // 3
}

- (void) loginSuccess
{

#ifndef ServerVersion
    //只有是 普通版本 在个人页面才显示退出按钮。
    UIButton *rightBtn = [[[UIButton alloc] init] autorelease];
    [rightBtn addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setImage:[UIImage imageNamed:@"icon_RCUserVC_logout"] forState:UIControlStateNormal];
    
    self.navigationItem.rightBarButtonItem = [self createRightBarButtonItemWithButton:rightBtn];
#endif
    
    [self hideContentController:[_controllers objectAtIndex:0]];
    [self displayContentController:[_controllers objectAtIndex:1]];
}

- (void) logoutSuccess
{
    self.navigationItem.rightBarButtonItem = nil;
    [[GloableEngineService sharedInstance]cleanLoginInfo];

    [self hideContentController:[_controllers objectAtIndex:1]];
    [self displayContentController:[_controllers objectAtIndex:0]];

}

- (void) logout
{
    [[GloableEngineService sharedInstance]cleanLoginInfo];
    [self logoutSuccess];
}


@end
